package com.sena.crawler.util;

import org.apache.commons.lang3.StringUtils;

import java.io.InputStream;
import java.util.*;

/**
 * @author seven
 * @create 2021-07-01 9:19
 **/
public class ReadProperties {

    public static List<String>  getUrlList(String key) {
        String url = null;
        Properties prop = new Properties();
        List<String> urlList = new ArrayList<>();
        try {
            // 读取属性文件xxxxx.properties
            ClassLoader classLoader = ReadProperties.class.getClassLoader();
            InputStream in = classLoader.getResourceAsStream("builder.properties");
            prop.load(in);
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                if (it.next().equals(key)) {
                    url = prop.getProperty(key);
                    if (StringUtils.isNotBlank(url)) {
                        if (url.contains(",")) {
                            urlList.addAll(Arrays.asList(url.split(",")));
                        } else {
                            urlList.add(url);
                        }
                    }
                }
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urlList;
    }

    public static String getValueByKey(String key,String configuration) {
        String value = null;
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件xxxxx.properties
            InputStream in = classLoader.getResourceAsStream(configuration);
            prop.load(in); /// 加载属性列表
            value = prop.getProperty(key);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
    public static String getRedisUrlValue(String urlName) {
        String url = null;
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件xxxxx.properties
            InputStream in = classLoader.getResourceAsStream("redis.properties");
            prop.load(in); /// 加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                if (it.next().equals(urlName)) {
                    url = prop.getProperty(urlName);
                }
            }
            in.close();
        } catch (Exception e) {

        }
        return url;
    }
    public static String getJDBCUrlValue(String urlName) {
        String url = null;
        Properties prop = new Properties();
        try {
            ClassLoader classLoader = ReadProperties.class.getClassLoader();// 读取属性文件xxxxx.properties
            InputStream in = classLoader.getResourceAsStream("jdbc.properties");
            prop.load(in); /// 加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                if (it.next().equals(urlName)) {
                    url = prop.getProperty(urlName);
                }
            }
            in.close();
        } catch (Exception e) {

        }
        return url;
    }
    public static void main(String[] args) {
        System.out.println(ReadProperties.getUrlList("whiteurl"));
    }
}
